<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>		
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					 // 查询关键词
					      query: '',
					      // 商品分类Id
					      cid: '',
					      // 页码值
					      pagenum: 1,
					      // 每页显示多少条数据
					      pagesize: 10
				},
				goodsList:[] ,// 列表数据
				total:0 ,//为了实现分页的功能,
				isLoading:false //节流阀
			};
		},
		onLoad(options) {
			console.log(options);
		  // 将页面参数转存到 this.queryObj 对象中
		  this.queryObj.query = options.query || ''
		  this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		methods:{
			// 获取商品列表数据
			async getGoodsList(cb) {
				// 打开节流阀
				this.isLoading = true
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				// console.log(res);
				this.isLoading = false 
				// 短路运算符
				cb && cb()
				if(res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList,...res.message.goods]
				this.total= res.message.total
			},
			gotoDetail(goods) {
				console.log('3333')
				uni.navigateTo({ 
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			},
		},
		// 上拉加载
		onReachBottom() {
			// 判断所有数据是否加载完毕
			if(this.queryObj.pagenum * this.queryObj.pagesize >=this.total) return uni.$showMsg('数据请求完毕！！！')
			// 页码自增
			if(this.isLoading) return
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		// 下拉刷新
		onPullDownRefresh(){
			// 重置关键数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading=false
			this.goodsList = []
			// 重新发起请求
			this.goodsList(() => uni.stopPullDownRefresh())
		},
		
	}
</script>

<style lang="scss">

</style>
